package com.xuetang9.t9.hotel.mapper.sql.impl;

import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.ArriveCondition;
import com.xuetang9.t9.hotel.mapper.sql.IMapperSql;
import com.xuetang9.t9.hotel.utils.OverallConstant;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Pang
 */
public class ArriveMapperSql implements IMapperSql {
    @Override
    public String selectByAll(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"*",true);
    }

    @Override
    public String selectByCount(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"COUNT(*)",false);
    }

    public String update(Map map){
        Integer id = (Integer)map.get("id");
        if(id == null){
            return null;
        }
        SQL sql = new SQL();
        sql.UPDATE("arrive_id").SET("checktype_id = "+ OverallConstant.CHECK_TYPE_EMPTY);
        sql.WHERE("arrive_id = "+id);
        return sql.toString();
    }
    private String getCondition(ICondition condition,String select,boolean isPage) {
        SQL sql = new SQL();
        sql.SELECT(select).FROM("g_blacklist");
        if(null == condition){
            return sql.toString();
        }
        ArriveCondition key = null;
        if(condition instanceof ArriveCondition){
            key = (ArriveCondition)condition;
        }else{
            throw  new RuntimeException("condition type Don't match");
        }
        if(null != key){
            if(null != key.getRoomNum() && !key.getRoomNum().isEmpty()){
                sql.WHERE("room_id IN ((SELECT room_id FROM g_roominfo WHERE room_num LIKE '% "+key.getRoomNum()+"%'))");
            }
            if(null != key.getGuestName() && !key.getGuestName().isEmpty()){
                sql.WHERE("guest_id IN ((SELECT guest_id FROM g_guest WHERE guest_name LIKE '% "+key.getGuestName()+"%'))");
            }
            if(null != key.getGuestNum() && !key.getGuestNum().isEmpty()){
                sql.WHERE("guest_id IN ((SELECT guest_id FROM g_guest WHERE card_num LIKE '% "+key.getGuestNum()+"%'))");
            }
            if(null != key.getArriveDate() && !key.getArriveDate().isEmpty()){

            }
            if(key.judge() && isPage){
                sql.LIMIT(key.getCurrPage()+","+key.getPageSize());
            }
        }
        return sql.toString();
    }

}
